<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\common\logic;
include_once '../extend/alidb/aliyun-openapi-php-sdk-master/aliyun-php-sdk-core/Config.php';
use Sts\Request\V20150401 as Sts;
define("REGION_ID", "cn-beijing");
define("ENDPOINT", "sts.cn-beijing.aliyuncs.com");
/**
 * oss相关逻辑
 * @package app\common\logic
 */
class Stslogic {

    //private $accessKeyId        = 'LTAI4FmtZLM3wactLdEi52tP';
    //private $accessKeySecret    = '4TcKFrb5s8ZwfOxHah3x4MxWMCQuA2';
    private $accessKeyId        = 'LTAI4G9KPip2LSwWXN7XxcYe';
    private $accessKeySecret    = 'TVsIcAtwpteTVarJe4T1JqbkJD218x';
    private $endpoint           = "muxiaopu2020.oss-cn-beijing.aliyuncs.com";// Endpoint以杭州为例，其它Region请按实际情况填写。
    private $bucket             = 'muxiaopu2020';// 存储空间名称
    private $timeout            = 3600;// 有效期

    /**
     * @expectedException \AlibabaCloud\Client\Exception\ServerException
     * @expectedExceptionMessageRegExp /EntityNotExist.Role: The specified Role not exists/
     * @throws ClientException
     * @throws ServerException
     */
    public function testSts(){
        try {
            // 只允许 RAM 用户使用角色
            \DefaultProfile::addEndpoint(REGION_ID, REGION_ID, "Sts", ENDPOINT);
            $iClientProfile = \DefaultProfile::getProfile(REGION_ID, $this ->accessKeyId, $this ->accessKeySecret);
            $client = new \DefaultAcsClient($iClientProfile);
            // 指定角色 ARN
            $roleArn = "acs:ram::1420391839382648:role/muxiaomu";
            // 在扮演角色时，添加一个权限策略，进一步限制角色的权限
            // 以下权限策略表示拥有可以读取所有 OSS 的只读权限
            $policy=<<<POLICY
            {
            "Statement": [
                {
                "Action": [
                    "oss:Get*",
                    "oss:List*",
                    "oss:*"
                ],
                "Effect": "Allow",
                "Resource": "*"
                }
            ],
            "Version": "1"
            }
POLICY;
            $request = new Sts\AssumeRoleRequest();
            // RoleSessionName 即临时身份的会话名称，用于区分不同的临时身份
            $request->setRoleSessionName("client_name");
            $request->setRoleArn($roleArn);
            $request->setPolicy($policy);
            $request->setDurationSeconds(3600);

            $response = $client->getAcsResponse($request);
            return array_out(200, 'success', $response);

            } catch (OssException $e) {
                return array_out(-9999, 'error', $e->getMessage());
                //print $e->getMessage();
            }
    }

    
}
